package cn.antscript.FaceDetector.view.component 
{
	import flash.display.Bitmap;
	import flash.display.BitmapData;
	import flash.display.Sprite;
	import flash.events.TimerEvent;
	import flash.media.Camera;
	import flash.media.Video;
	import flash.utils.Timer;
	
	import gs.TweenLite;
	import gs.easing.*;
	
	import jp.maaash.ObjectDetection.ObjectDetector;
	import jp.maaash.ObjectDetection.ObjectDetectorEvent;
	import jp.maaash.ObjectDetection.ObjectDetectorOptions;
	
	/**
	 * ...
	 * @author AntScript
     * @blog  http://www.antscript.cn
	 */
	public class GlassesDetector extends Sprite
	{
		[Embed(source = '../../../../../../lib/glassesImg/1.swf')]
		private var Glasses:Class;
		
		
		private static const _cameraWidth:int = 320;
		private static const _cameraHeight:int = 240;
		private static const _cameraFPS:int = 24;
		
		
		private static const _detectionTime:int = 50;
		private var _detectionTimer:Timer;
		private static const _noFaceTimeOut:int = 500;
		private var _noFaceTimer:Timer;
		
		
		private var _detector:ObjectDetector;
		private var _options:ObjectDetectorOptions;
		private var _detectorBitmap:Bitmap;
		
		
		private var _video:Video;
		
		private var _glasses:Array = [];
		
		public function GlassesDetector() 
		{
			init(); 
		}
		
		private function init():void
		{
			initTime();
			initDetector();
			initCamera();
			
			_detector.addEventListener(ObjectDetectorEvent.DETECTION_COMPLETE, onDetectionCom);
			
			_detectionTimer.start();
		}
		
		private function onDetectionCom(e:ObjectDetectorEvent):void 
		{
			if (e.rects.length == 0) return;
			
			_noFaceTimer.stop( );
			_noFaceTimer.start();
			
			var length:int = e.rects.length;
			for (var i : int = 0; i < length ; i++) {
				
				if(_glasses[i] == null){
					_glasses[i] = new Glasses();
					addChild(_glasses[i]);
				}
				
				TweenLite.to( _glasses[i] , .5, {
					autoAlpha:1,
					x:e.rects[i].x*_video.scaleX + _video.x,
					y:e.rects[i].y*_video.scaleY+e.rects[i].height*_video.scaleY*0.1,
					width:e.rects[i].width*_video.scaleX,
					height:e.rects[i].height*_video.scaleY*0.25,
					ease:Cubic.easeOut	
				} );	
			}
			
			if (e.rects.length < _glasses.length) {
				 
				for (var j : int = e.rects.length; j < _glasses.length; j++) {
					
					TweenLite.to( _glasses[j] , .5, {alpha:0} );					
				}
			}
		}
		
		private function initTime():void
		{
			_detectionTimer = new Timer(_detectionTime);
			_detectionTimer.addEventListener(TimerEvent.TIMER, onDetection);
			
			_noFaceTimer = new Timer(_noFaceTimeOut);
			_noFaceTimer.addEventListener(TimerEvent.TIMER,onNoFace);
		}
		
		private function onDetection(e:TimerEvent):void 
		{
			_detectorBitmap = new Bitmap( new BitmapData( _video.width, _video.height, false ) );
			_detectorBitmap.bitmapData.draw( _video );
			_detector.detect( _detectorBitmap );
		}
		
		private function onNoFace(e:TimerEvent):void 
		{
			_noFaceTimer.stop();
			var length:int = _glasses.length;
			for (var i : int = 0; i < length; i++) {
					_glasses[i].visible = false;
					TweenLite.to(_glasses[i] , .5, {alpha:0} );	
				}
		}
		
		private function initDetector():void
		{
			_detector = new ObjectDetector();
			_detector.options = getDetectorOptions();
			_detector.loadHaarCascades( "../lib/face.zip" );
		}
		private function getDetectorOptions () : ObjectDetectorOptions {
			
			_options = new ObjectDetectorOptions();
			_options.min_size = 50;
			_options.startx = ObjectDetectorOptions.INVALID_POS;
			_options.starty = ObjectDetectorOptions.INVALID_POS;
			_options.endx = ObjectDetectorOptions.INVALID_POS;
			_options.endy = ObjectDetectorOptions.INVALID_POS;
			return _options;
			
		}
		
		private function initCamera():void
		{
			var camera:Camera = Camera.getCamera();
			
			if (camera) {
				camera.setMode(_cameraWidth, _cameraHeight, _cameraFPS);
				_video = new Video(320, 240);
				_video.attachCamera(camera);
				addChild(_video);
			}
			
			
		}
		
	}
	
}